package com.ckk.rabbitmq.work;

import com.ckk.rabbitmq.config.ConnectionConfig;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * @author 陈昆昆
 * @version 1.0
 * @date 2020/6/20 14:49
 */
public class Send {
    private final static String QUEUE_NAME = "test_work_queue";

    public static void main(String[] argv) throws Exception {
        //获取连接
        Connection connection = ConnectionConfig.getconnection();

        //创建管道
        Channel channel = connection.createChannel();

        // 声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // 循环发布任务
        for (int i = 0; i < 50; i++) {
            // 消息内容
            String message = "task .. " + i;
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            System.out.println(" [x] Sent '" + message + "'");

            Thread.sleep(i * 2);
        }
        // 关闭通道和连接
        channel.close();
        connection.close();
    }
}